package com.sctvcloud.dongqu.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.util.LittleLifeManager;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.base.BaseFragment;
import com.sctvcloud.dongqu.beans.MessageInfo;
import com.sctvcloud.dongqu.beans.NewsItem;
import com.sctvcloud.dongqu.ui.activities.NewesDetailActivity;
import com.sctvcloud.dongqu.ui.adapter.holder.BaseAbsHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.LiveHomeAdvanceHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.LiveHomeCountDownHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.LiveHomeItemHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.OnlyTitleHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.VideoListHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.VideoPlayHolder;
import com.sctvcloud.dongqu.ui.util.IListShowData;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;
import com.sctvcloud.dongqu.ui.widget.BasePlayerView;

import java.util.ArrayList;
import java.util.List;

public class VideoPlayAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int VIEWTYPE_ADVANCE = 0;//预告
    public static final int VIEWTYPE_TITLE = 1;//标题
    public static final int VIEWTYPE_LIVE = 2;//直播的item
    public static final int VIEWTYPE_ADVANCE_LIST = 4;//直播预告列表
    private OnItemInternalClick onItemSubscribeClick;

    private LittleLifeManager littleLifeManager;
    private BaseActivity activity;
    private BaseFragment fragment;
    private ArrayList<Pair<String, String>> pairs;
    private VideoPlayHolder.NewsItemClickListener listener;
    private VideoPlayHolder.NewsItemTitleClickListener titleClickListener;
    private VideoPlayHolder.NewsItemDoUpClickListener doUpClickListener;
    private List<NewsItem> dataList;
    private Context context;
    private OnMuteChangeListener muteChangeListener;
    private Handler handler = new Handler();
    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    public void setFragment(BaseFragment fragment) {
        this.fragment = fragment;
    }

    public void setPairs(ArrayList<Pair<String, String>> pairs) {
        this.pairs = pairs;
    }

    public OnItemInternalClick getOnItemSubscribeClick() {
        return onItemSubscribeClick;
    }

    public void setOnItemSubscribeClick(OnItemInternalClick onItemSubscribeClick) {
        this.onItemSubscribeClick = onItemSubscribeClick;
    }

    public void setMuteChangeListener(OnMuteChangeListener muteChangeListener) {
        this.muteChangeListener = muteChangeListener;
    }

    public void setNewsItemListener(VideoPlayHolder.NewsItemClickListener listener) {
        this.listener = listener;
    }

    public void setTitleClickListener(VideoPlayHolder.NewsItemTitleClickListener titleClickListener) {
        this.titleClickListener = titleClickListener;
    }

    public void setDoUpClickListener(VideoPlayHolder.NewsItemDoUpClickListener doUpClickListener) {
        this.doUpClickListener = doUpClickListener;
    }

    public VideoPlayAdapter(Context context, List<NewsItem> list) {
        dataList = new ArrayList<>();
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View v = v = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_play_item, parent, false);
        holder = new VideoPlayHolder(v);
        return holder;
    }

    public void setData(List<NewsItem> list) {
        if (null != list) {
            dataList = list;
        }
        notifyDataSetChanged();
    }

    public void addData(List<NewsItem> list) {
        int pos = getItemCount();
        if (null != list) {
            dataList.addAll(list);
        }
        if (pos > 0) {
            notifyItemRangeChanged(pos,getItemCount());
        }
    }
    public void updateData(List<NewsItem> list) {
        if (null != list) {
            dataList = list;
        }
    }
    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final NewsItem info = dataList.get(position);
        final VideoPlayHolder viewHolder = (VideoPlayHolder) holder;
        String title = info.getNewsTitle();
        if(title.length()>40){
            title= title.substring(0,40)+"...";
        }
        viewHolder.title.setText(title);
//        viewHolder.title.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (null != titleClickListener) {
//                    titleClickListener.OnItemTitleClick(position,info);
//                }
//            }
//        });


        if (null != viewHolder.videoPlayer && !TextUtils.isEmpty(info.getNewsMediaUrl())) {
            viewHolder.setCanPlay(true);
            viewHolder.noUrlImg.setVisibility(View.GONE);
            viewHolder.videoPlayer.setVisibility(View.VISIBLE);
//            viewHolder.videoPlayer.setMute(false);
            viewHolder.videoPlayer.setMuteChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (null != muteChangeListener) {
                        muteChangeListener.OnMuteChange(isChecked);
                    }
                }
            });
            viewHolder.videoPlayer.setOnPlayerStatusListener(new BasePlayerView.OnPlayerStatusChanged() {
                @Override
                public void OnPlaying() {

                }

                @Override
                public void OnError() {

                }

                @Override
                public void OnStop() {

                }

                @Override
                public void OnEnd() {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (viewHolder.videoPlayer  != null) {
                                viewHolder.playVideo();
                            }
                        }
                    }, 100);
                }
            });
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (viewHolder.videoPlayer.getThumbImge() != null  && null!=activity && !activity.isDestroyed()) {
                        GlideUtil.getGlideWithLarge169DefNotFit(context, info.getNewsImage()).into(viewHolder.videoPlayer.getThumbImge());
                    }

                    viewHolder.videoPlayer.setUp(info.getNewsMediaUrl(), false, "");
                    viewHolder.videoPlayer.onVideoPause();

                }
            }, 200);
        } else {
            viewHolder.setCanPlay(false);
            viewHolder.videoPlayer.setVisibility(View.GONE);
            viewHolder.noUrlImg.setVisibility(View.VISIBLE);
            GlideUtil.getGlideWithLarge169DefNotFit(context, info.getNewsImage()).into(viewHolder.noUrlImg);
        }
        viewHolder.coverImg.setVisibility(View.VISIBLE);
        viewHolder.coverImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                viewHolder.videoPlayer.startPlayLogic();
//                viewHolder.coverImg.setVisibility(View.GONE);
                if (null != listener) {
                    listener.OnItemClick(position);
                }
            }
        });
        if (info.isDigg()) {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.icon_compere_btn_press);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.doUp.setDrawables(drawable, null, null, null);
            viewHolder.doUp.setTextColor(context.getResources().getColor(R.color.colorTxRedMain_));
        } else {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.icon_compere_btn_white);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.doUp.setDrawables(drawable, null, null, null);
            viewHolder.doUp.setTextColor(context.getResources().getColor(R.color.colorOpinionSelectNo));
        }
        viewHolder.doUp.setText(""+info.getCountDigg());
        viewHolder.doUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null!=doUpClickListener) {
                    doUpClickListener.OnItemDoUpClick(position, info.getNewsId());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    public interface OnMuteChangeListener {
        void OnMuteChange(boolean isMute);
    }
}
